package com.springboot.controller;

import com.springboot.feign.UserFeign;
import com.springboot.model.po.Credit;
import com.springboot.service.CreditService;

import util.VerifyUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RefreshScope	//监听动态刷新
@RestController
@RequestMapping("/credits")
public class CreditController {
    @Autowired
    private CreditService creditService;
    @Autowired
    private UserFeign userFeign;

    @GetMapping("/totalNum/{userId}")
    //累计积分可能会用于系统排名显示，所以不需要token
    public ResponseEntity getTotalCreditByUserId(@PathVariable String userId) throws Exception {
    	ResponseEntity result = userFeign.getUserById(userId);
    	if(result.getStatusCode().equals(HttpStatus.NOT_FOUND))
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body("NOT FOUND");
        return ResponseEntity.ok(creditService.getTotalCreditByUserId(userId));
    }

    @GetMapping("/availableCredits/{userId}")
    public ResponseEntity getAvailableCreditByUserId(@PathVariable String userId , @RequestParam String token) throws Exception {
    	//可用积分明细（需要身份验证）
    	String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !userId.equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
    	List<Credit> list = creditService.getAvailableCreditByUserId(userId);
    	if(list.size() == 0 || list == null) {
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No available credits!");
    	}else {
    		return ResponseEntity.ok(list);
    	}
    		 
    }

    @GetMapping("/details/{userId}")
    public ResponseEntity getCreditByUserId(@PathVariable String userId , @RequestParam String token) throws Exception {
    	//获得当前用户的全部积分流水明细(需要验证身份)
    	String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !userId.equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
    	List<Credit> list = creditService.getCreditByUserId(userId);
        if(list.size() == 0 || list == null) {
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No available credits!");
    	}else {
    		return ResponseEntity.ok(list);
    	}
    }

    @GetMapping("/channelType")
    public ResponseEntity getCreditByUserIdAndChannelType(@RequestParam("channelType") Integer channelType , @RequestParam("userId") String userId , @RequestParam("token") String token) throws Exception {
    	//获得当前用户指定类型的全部积分流水明细
    	String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !userId.equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
    	List<Credit> list = creditService.getCreditByUserIdAndChannelType(userId, channelType);
    	if(list.size() == 0 || list == null) {
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No available credits!");
    	}else {
    		return ResponseEntity.ok(list);
    	}
    }
    
    //插入记录通过订单状态改变或者购买优惠券实现积分的流水记录所以这里不提供外部接口

//    @PostMapping
//    public int insertCredit(@RequestBody Credit credit) throws Exception {
//    	//userId，channelType，num
//    	//向积分流水表中添加一条记录
//        return creditService.insertCredit(credit);
//    }
}
